// DOM加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 初始化功能
    initPageTransitions();
    initThemeToggle();
    initLanguageSwitcher();
    
    // 获取联系表单元素
    const contactForm = document.getElementById('contact-form');
    
    // 添加表单提交事件监听器
    if (contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 获取表单数据
            const name = document.getElementById('name').value;
            const email = document.getElementById('email').value;
            const message = document.getElementById('message').value;
            
            // 简单验证
            if (name && email && message) {
                // 在实际项目中，这里会发送数据到服务器
                alert(getLocalizedString('messageSent'));
                contactForm.reset();
            } else {
                alert(getLocalizedString('fillAllFields'));
            }
        });
    }
    
    // 添加导航链接平滑滚动效果
    const navLinks = document.querySelectorAll('nav a');
    navLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            const targetId = this.getAttribute('href');
            const targetSection = document.querySelector(targetId);
            
            if (targetSection) {
                targetSection.scrollIntoView({
                    behavior: 'smooth'
                });
            }
        });
    });
    
    // 添加一个简单的动画效果到作品项
    const portfolioItems = document.querySelectorAll('.portfolio-item');
    portfolioItems.forEach((item, index) => {
        // 设置延迟以创建级联效果
        item.style.transitionDelay = `${index * 0.1}s`;
        
        // 当元素进入视口时添加可见类
        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    entry.target.classList.add('visible');
                }
            });
        }, { threshold: 0.1 });
        
        observer.observe(item);
    });
    
    // 为证书添加动画效果
    const certificates = document.querySelectorAll('.certificate');
    certificates.forEach((certificate, index) => {
        // 当元素进入视口时添加可见类
        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    setTimeout(() => {
                        entry.target.style.transform = 'translateY(-5px)';
                        entry.target.style.boxShadow = '0 5px 15px rgba(0,0,0,0.1)';
                    }, index * 100);
                }
            });
        }, { threshold: 0.1 });
        
        observer.observe(certificate);
    });
    
    // 为社交媒体链接添加交互效果
    const socialLinks = document.querySelectorAll('.social-link');
    socialLinks.forEach(link => {
        link.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-5px)';
        });
        
        link.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
        });
    });
});

// 页面过渡动画
function initPageTransitions() {
    const fadeElements = document.querySelectorAll('section, h1, h2, h3, p, .portfolio-item, .certificate, .social-link');
    
    fadeElements.forEach((element, index) => {
        element.classList.add('fade-in');
        element.style.transitionDelay = `${index * 0.1}s`;
    });
    
    // 当页面加载完成后，触发动画
    setTimeout(() => {
        fadeElements.forEach(element => {
            element.classList.add('appear');
        });
    }, 100);
}

// 主题切换功能
function initThemeToggle() {
    const themeToggle = document.getElementById('theme-toggle');
    const themeIcon = document.querySelector('.theme-icon');
    
    // 检查本地存储中的主题设置
    const currentTheme = localStorage.getItem('theme');
    if (currentTheme === 'dark') {
        document.body.classList.add('dark-mode');
    }
    
    themeToggle.addEventListener('click', function() {
        document.body.classList.toggle('dark-mode');
        
        // 保存主题设置到本地存储
        if (document.body.classList.contains('dark-mode')) {
            localStorage.setItem('theme', 'dark');
        } else {
            localStorage.setItem('theme', 'light');
        }
    });
}

// 语言切换功能
function initLanguageSwitcher() {
    const languageSelector = document.getElementById('language-selector');
    
    // 检查本地存储中的语言设置
    const currentLanguage = localStorage.getItem('language') || 'zh';
    languageSelector.value = currentLanguage;
    
    // 初始化语言
    switchLanguage(currentLanguage);
    
    languageSelector.addEventListener('change', function() {
        switchLanguage(this.value);
        // 保存语言设置到本地存储
        localStorage.setItem('language', this.value);
    });
}

// 语言包
const translations = {
    zh: {
        home: '首页',
        portfolio: '作品',
        about: '关于',
        contact: '联系',
        welcome: '欢迎来到我的作品集',
        intro: '这里展示我的项目和技能',
        messageSent: '感谢您的消息！我会尽快回复您。',
        fillAllFields: '请填写所有必填字段。'
    },
    en: {
        home: 'Home',
        portfolio: 'Portfolio',
        about: 'About',
        contact: 'Contact',
        welcome: 'Welcome to My Portfolio',
        intro: 'Here are my projects and skills',
        messageSent: 'Thank you for your message! I will reply as soon as possible.',
        fillAllFields: 'Please fill in all required fields.'
    }
};

// 切换语言
function switchLanguage(lang) {
    const elements = document.querySelectorAll('[data-i18n]');
    
    elements.forEach(element => {
        const key = element.getAttribute('data-i18n');
        if (translations[lang] && translations[lang][key]) {
            if (element.tagName === 'INPUT' || element.tagName === 'TEXTAREA') {
                element.placeholder = translations[lang][key];
            } else {
                element.textContent = translations[lang][key];
            }
        }
    });
    
    // 特殊处理主页标题和介绍
    const homeTitle = document.querySelector('#home h1');
    const homeIntro = document.querySelector('#home p');
    
    if (homeTitle) homeTitle.textContent = translations[lang].welcome;
    if (homeIntro) homeIntro.textContent = translations[lang].intro;
}

// 获取本地化字符串
function getLocalizedString(key) {
    const currentLanguage = localStorage.getItem('language') || 'zh';
    return translations[currentLanguage][key] || key;
}

// 简单的交互功能示例
function toggleMenu() {
    const nav = document.querySelector('nav ul');
    nav.classList.toggle('show');
}

// 页面滚动时添加效果
window.addEventListener('scroll', function() {
    const header = document.querySelector('header');
    if (window.scrollY > 50) {
        header.style.boxShadow = '0 2px 5px rgba(0,0,0,0.1)';
    } else {
        header.style.boxShadow = 'none';
    }
});